package com.huixi.controller;

import com.huixi.entity.Item;
import com.huixi.service.mapper.ItemMapper;
import com.huixi.service.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private ItemMapper itemMapper;

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "/add/{itemId}")
    public String addCart(@PathVariable Long itemId,
                          @RequestParam(defaultValue = "1") int num,
                          HttpServletRequest request,
                          HttpServletResponse response) {


        return "cartSuccess";
    }

    @RequestMapping(value = "/cart")
    public String cart(HttpServletRequest request) {
        List<String> itemIdList = Arrays.asList("536563", "562379");
        List<Item> cartList = itemMapper.selectBatchIds(itemIdList);
        for (Item cart : cartList) {
            cart.setNum(1);
        }

        request.setAttribute("cartList", cartList);
        return "cart";
    }
}
